package com.hongdee.scm.common.controller;

import com.hongdee.scm.common.service.ICommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Mr. Xu on 2017/7/3.
 */
@RestController
@RequestMapping("/scm/common")
public class CommonController {
    @Autowired
    private ICommonService iCommonService;
    @RequestMapping("/invoiceSerialNumber")
    @ResponseBody
    public Map invoiceSerialNumber(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token) throws Exception {

        Map map = new HashMap();
        String serialNumber = this.iCommonService.serialNumber("0");
        String time = this.iCommonService.currentTime();
        map.put("serialNu", time+serialNumber);
        //查詢一次刷新一次流水
        this.iCommonService.updateSerialNumber(serialNumber);
        return map;
    }

    /**
     * 更新流水号
     * @param uid
     * @param token
     * @param currentSerial
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateSerialNumber")
    @ResponseBody
    public Map updateSerialNumber(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestParam(name = "currentSerial" ,defaultValue = "")String currentSerial) throws Exception {
        Map map = new HashMap();
        this.iCommonService.updateSerialNumber(currentSerial.substring(currentSerial.length()-4,currentSerial.length()));
        map.put("success", true);
        return map;
    }
}
